/*
  * superagent为js写的库，ts中引用时会报错，所以需要根据提示来安装: @types/superagent
  * npm install @types/superagent -D
*/
import fs from 'fs';
import path from 'path';
import superagent from 'superagent';
import cheerio from 'cheerio';
interface Course {
  title: string;
  count: number | string;
}
interface CourseResult {
  time: number;
  data: Course[];
}
interface Content {
  [propsName: number]: Course[];
}
class Crowller {
  // url为网页的网址
  private url = 'http://localhost:8081/';
  constructor() {
    this.getRawHtml();
  }
  async getRawHtml() {
    const res = await superagent.get(this.url);
    this.getJsonInfo(res.text);
  }
  getJsonInfo(html: string) {
    const data: Course[] = [];
    const $ = cheerio.load(html);
    const items = $('.list-item');
    items.map((index, element) => {
      const item = $(element).find('.title');
      const title = item.eq(0).text();
      const count = item.eq(1).text().split(':')[1]
      data.push({ title, count})
    });
    const result = {
      time: new Date().getTime(),
      data
    };
    this.generateJsonFile(result);
  }
  generateJsonFile(content: CourseResult) {
    const filePath = path.resolve(__dirname, '../data/data.json');
    let fileContent: Content = {};
    if(fs.existsSync(filePath)) {
      fileContent = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
    }
    fileContent[content.time] = content.data;
    fs.writeFileSync(filePath, JSON.stringify(fileContent));
  }
}

const obj = new Crowller();